using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Jabr.Logic.Exceptions.Handling
{
    public sealed class ExceptionInfo : AbstractInfo
    {
        private Dictionary<string, string> ReadProperties(Exception e)
        {
            Dictionary<string, string> ret = new Dictionary<string, string>();
            Type et = e.GetType();
            ret.Add("Type", et.Name);
            PropertyInfo[] props = et.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.CanRead)
                {
                    object val = prop.GetGetMethod().Invoke(e, null);
                    if (val != null)
                        ret.Add(prop.Name, val.ToString());
                }
            }
            return ret;
        }

        public ExceptionInfo(Exception e)
        {
            m_properties = ReadProperties(e);
            if (e.InnerException != null)
            {
                m_inner = new ExceptionInfo(e.InnerException);
            }
            else
            {
                m_inner = null;
            }
        }

        public override string ToString()
        {
            return m_properties["Type"];
        }
    }
}
